#!/bin/bash
#
# Script to turn those metapost/metafun/context graphs into eps/pdf format
# Usage: figure-latex --help | --all | file[.tex] [file[.tex]] ...
#   --help: display the help (this text)
#   --all : generate figures for all tex files in the current directory,
#   file  : only for specified figures.
#
version='$Id: figure-latex,v 1.10 2004/08/09 09:32:21 msneep Exp $'

# some variables
theFiguresList=''
theErrorMessageFile="error-messages-$$.txt"
makePdf='0'
makeMps='0'
makeEps='0'
useSpecial='0'
RemoveOthers='0'
workingdirectory='.'
makeAll='0'
progname=`basename $0`

# parse command line
while [ $# -gt 0 ]
do
	case $1 in
		-h*|--h*)                      # help item
			cat > /dev/stderr <<EOF
Usage: $progname [-help] | [-version] | [-mps] [-eps] [-pdf] [-delete] [-all | files] 
           [-special] [-dir workingdirectory]
   -help: display the help (this text)
   -version: display version information.
   -all : generate figures for all metapost files in the current directory,
   -mps : make the mps files (default, for pdflatex)
   -eps : make the eps files (for latex)
   -pdf : make pdf files, for a faster, but bigger pdflatex
   -del : remove other files
   -special : use a special method to produce a pdf file here
   -dir dir : go to this directory first
   file : only for specified figures.
EOF
			exit 0 ;;
		-p*)
			makePdf='1' ;;
		-m*)
			makeMps='1';;
		-e*)
			makeEps='1' ;;
		-del*)
			RemoveOthers='1' ;;
		-dir*)
			shift
			workingdirectory="$1" ;;
		-a*)
			makeAll='1' ;;
		-ver*)
			echo $version | tr -d '$' | awk '{printf "%s %s (%s, %s)\n", $1, $3, $4, $5}' ;
			exit 0 ;;
		-sp*)
			useSpecial='1' ;;
		-*)                        # unrecognized option
			echo "Error: option not recognized: $1" > /dev/stderr
			$0 --help;
			exit 1 ;;
		*)                        # a file
			if [ -f "${1%.*}.mp" ]
			then 
				theFiguresList="$theFiguresList \"${1%.*}.mp\""
			else
				echo "Error: file ${1%.*}.mp not found" > /dev/stderr
				$0 --help
				exit 2
			fi ;;
	esac
	shift
done

if [ "$makeMps" = "0" -a "$makeEps" = "0" -a "$makePdf" = "0" ]
then
	echo "No formats selected, will generate .mps"
	makeMps='1';
fi

pushd "$workingdirectory"  > /dev/null 2>&1

if [ "$makeAll" == '1' ]
then
	theFiguresList=`ls -1 *.mp`
fi

for fig in $theFiguresList
do
	directoryOfFile=`dirname "${fig}"`
	currentFileName=`basename "${fig}"`
	if [ "$directoryOfFile" = "$fig" ] 
	then 
		directoryOfFile="." 
	fi
	
	if [ -d "$directoryOfFile" -a "$directoryOfFile" != "." ] 
	then 
		pushd "$directoryOfFile"  > /dev/null 2>&1
	fi
	
	if [ "$RemoveOthers" = "1" ]
	then
		if [ -f "${currentFileName%.*}.eps" ] 
		then 
			rm "${currentFileName%.*}.eps"
		fi
		if [ -f "${currentFileName%.*}.mps" ] 
		then
			rm "${currentFileName%.*}.mps"
		fi
		if [ -f "${currentFileName%.*}.pdf" ] 
		then 
			rm "${currentFileName%.*}.pdf"
		fi
	fi

	echo "Running on figure ${currentFileName%.*}"
	# process the temporary file
	mpost -interaction=nonstopmode \
		-file-line-error-style \
		-tex=latex \
		"${currentFileName%.*}.mp" > /dev/null 2>&1
	
	mystat=$?
	if [ $mystat != 0 ] 
	then
		echo "${currentFileName%.*}.0 not generated" > /dev/stderr
		echo "${currentFileName%.*}.0 not generated" > "${theErrorMessageFile}"
		mv "${currentFileName%.*}.log" "${currentFileName%.*}.err.log"
	fi
	
	if [ "$makePdf" == "1" ] 
	then
		if [ -f "${currentFileName%.*}.0" ]
		then
			if [ "$useSpecial" = "1" ]
			then
				cp "${currentFileName%.*}.0" "${currentFileName%.*}.tmp.eps"
				pstopdf "${currentFileName%.*}.tmp.eps" # this is Apple's ps to pdf converter in 10.3 or later
				rm "${currentFileName%.*}.tmp.eps"
				pdftk "${currentFileName%.*}.tmp.pdf" output \
					"${currentFileName%.*}-0.pdf" compress
			else
				mptopdf "${currentFileName%.*}.0"  > /dev/null 2>&1
			fi
		fi
		
		# move the products to the final location
		if [ -f "${currentFileName%.*}-0.pdf" ]
		then
			mv "${currentFileName%.*}-0.pdf" "${currentFileName%.*}.pdf"
		else
			echo "${currentFileName%.*}.pdf not generated" > /dev/stderr
			echo "${currentFileName%.*}.pdf not generated" > "${theErrorMessageFile}"
		fi
	fi
	
	if [ "$makeMps" = "1" ] 
	then
		if [ -f "${currentFileName%.*}.0" ]
		then
			cp "${currentFileName%.*}.0" "${currentFileName%.*}.mps"
		else 
			echo "${currentFileName%.*}.mps not generated" > /dev/stderr
			echo "${currentFileName%.*}.mps not generated" > "${theErrorMessageFile}"
		fi
	fi
	
	if [ "$makeEps" = "1" ]
	then
		if [ -f "${currentFileName%.*}.0" ]
		then
			cp "${currentFileName%.*}.0" "${currentFileName%.*}.eps"
		else 
			echo "${currentFileName%.*}.eps not generated" > /dev/stderr
			echo "${currentFileName%.*}.eps not generated" > "${theErrorMessageFile}"
		fi
	fi

	# remove the crud
	if [  -f "${currentFileName%.*}.0" ]
	then
		rm "${currentFileName%.*}.0"
	fi
 	if [  -f "${currentFileName%.*}.mpx"  ]
	then
		rm "${currentFileName%.*}.mpx"
	fi
 	if [  -f "${currentFileName%.*}.log" ]
	then
		rm "${currentFileName%.*}.log"
	fi
	if [  -f "texnum.mpx" ]
	then
		rm "texnum.mpx"
	fi
	if [  -f "mptrace.tmp" ]
	then
		rm "mptrace.tmp"
	fi
	
	if [  -d "$directoryOfFile" -a "$directoryOfFile" != "."  ]
	then
		popd  > /dev/null 2>&1
	fi
done

if [  -f "${theErrorMessageFile}"  ]
then
	cat "${theErrorMessageFile}" > /dev/stderr
	rm "${theErrorMessageFile}"
	exit 1
fi

popd  > /dev/null 2>&1

exit 0
